package com.aconcagua.mvc.factory {
  import com.aconcagua.beans.Bean;
  import com.aconcagua.beans.BeanDefinitionFactory;
  import com.aconcagua.beans.BeanDefinitionSupport;
  import com.aconcagua.beans.ConstructorArg;
  import com.aconcagua.beans.Value;
  import com.aconcagua.factory.BeanFactory;
  import com.aconcagua.mvc.control.support.CommandFactory;
  
  import mx.core.IMXMLObject;
  
  //TODO: Change this implementation: This has to be a BeanFactory, when
  //      support for nested factories is implemented
  public class CommandFactory implements BeanDefinitionFactory, IMXMLObject {
    private var _id: String;
    private var _bean: BeanDefinitionSupport;

    public function initialized(document:Object, id:String): void {
      _id = id;
    }

    public function registerIn(beanFactory: BeanFactory): void {
      _bean = new BeanDefinitionSupport(com.aconcagua.mvc.control.support.CommandFactory, _id);
      _bean.scope = BeanDefinitionSupport.SCOPE_SINGLETON;
      _bean.addConstructorArg(new ConstructorArg(new Value(beanFactory)));
      
      beanFactory.registerBeanDefinition(_bean);
    }

    public function isRegisteredIn(beanFactory: BeanFactory): Boolean {
      return beanFactory.containsBean(_bean.name);
    }
    
    public function retrieveFrom(beanFactory: BeanFactory): Object {
      return beanFactory.getBean(_bean.name);
    }
  }
}